<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
%>
<html>
<head>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">

    <link href="jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>
    <script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/commons.js"></script>

    <script type="text/javascript">

        //默认情况下取消和保存按钮是隐藏的
        var cancelAndSaveBtnDefault = true;

        $(function () {
            //获取线索活动留言
            getClueRemark();

            //获取线索关联的市场活动
            getActivityClue()

            $("#remark").focus(function () {
                if (cancelAndSaveBtnDefault) {
                    //设置remarkDiv的高度为130px
                    $("#remarkDiv").css("height", "130px");
                    //显示
                    $("#cancelAndSaveBtn").show("2000");
                    cancelAndSaveBtnDefault = false;
                }
            });

            $("#cancelBtn").click(function () {
                //显示
                $("#cancelAndSaveBtn").hide();
                //设置remarkDiv的高度为130px
                $("#remarkDiv").css("height", "90px");
                cancelAndSaveBtnDefault = true;
            });

            $(".remarkDiv").mouseover(function () {
                $(this).children("div").children("div").show();
            });

            $(".remarkDiv").mouseout(function () {
                $(this).children("div").children("div").hide();
            });

            $(".myHref").mouseover(function () {
                $(this).children("span").css("color", "red");
            });

            $(".myHref").mouseout(function () {
                $(this).children("span").css("color", "#E6E6E6");
            });

            //给保存按钮绑定单击事件
            $("#saveClueRemarkBtn").on("click", function () {
                //获取留言内容
                let noteContent = $("#remark").val();
                //获取线索id
                var clueId = "${clue.id}";

                //判断是否为空
                if ("" === noteContent) {
                    alert("留言不能为空!");
                    return;
                }
                //发送ajax请求保存
                $.ajax({
                    url: "workbench/clueRemark/saveClueRemark.do",
                    data: {
                        clueId: clueId,
                        noteContent: noteContent
                    },
                    type: "post",
                    success: function (data) {
                        if (data.code === 1) {
                            alert("添加成功!");
                            //清空留言框内容
                            $("#remark").val("");
                            //刷新留言数据列表
                            getClueRemark();
                        } else {
                            alert(data.message);
                        }
                    }
                })
            });

            //给更新按钮绑定单击事件
            $("#updateRemarkBtn").on("click", function () {

                //获取更新的内容和id
                let id = $("#edit-id").val();
                let noteContent = $("#edit-noteContent").val();

                //判空
                if ("" === noteContent) {
                    alert("更新内容不能为空");
                    return;
                }

                //发送ajax请求
                $.ajax({
                    url: "workbench/clueRemark/updateClueRemark.do",
                    data: {
                        id: id,
                        noteContent: noteContent
                    },
                    type: "post",
                    success: function (data) {
                        if (data.code === 1) {
                            alert("更新成功!");
                            //关闭模态窗口
                            $("#editRemarkModal").modal("hide");
                            //刷新数据列表
                            getClueRemark();
                        } else {
                            alert(data.message);
                        }
                    }
                })
            });

            //给关联市场活动绑定单击事件
            $("#bundActivityBtn").on("click", function () {
                //打开模态窗口
                $("#bundModal").modal("show");

                //获取线索数据
                var clueId = "${clue.id}";

                //通过ajax获取数据
                $.ajax({
                    url:"workbench/clueRemark/getActivityNoBud.do",
                    data:{
                        clueId:clueId
                    },
                    type:"get",
                    success:function (data) {
                        //将数据加载到对应的div中
                        var htmlStr = "";
                        $.each(data, function (index, item) {
                            htmlStr += "<tr>";
                            htmlStr += "<td><input type=\"checkbox\" value=\""+item.id+"\"/></td>";
                            htmlStr += "<td>" + item.name + "</td>";
                            htmlStr += "<td>" + item.startDate + "</td>";
                            htmlStr += "<td>" + item.endDate + "</td>";
                            htmlStr += "<td>" + item.owner + "</td>";
                            htmlStr += "</tr>";
                        });
                        $("#tBody").html(htmlStr);
                    }
                })
            });

            //给查询按钮绑定单击事件
            $("#searchActivityName").on("change",function () {
                //获取查询的参数
                let activityName = $("#searchActivityName").val();

                //发送ajax请求
                //获取线索数据
                var clueId = "${clue.id}";

                //通过ajax获取数据
                $.ajax({
                    url:"workbench/clueRemark/getActivityNoBud.do",
                    data:{
                        clueId:clueId,
                        activityName:activityName
                    },
                    type:"get",
                    success:function (data) {
                        //将数据加载到对应的div中
                        var htmlStr = "";
                        $.each(data, function (index, item) {
                            htmlStr += "<tr>";
                            htmlStr += "<td><input type=\"checkbox\" value=\""+item.id+"\"/></td>";
                            htmlStr += "<td>" + item.name + "</td>";
                            htmlStr += "<td>" + item.startDate + "</td>";
                            htmlStr += "<td>" + item.endDate + "</td>";
                            htmlStr += "<td>" + item.owner + "</td>";
                            htmlStr += "</tr>";
                        });
                        $("#tBody").html(htmlStr);
                    }
                })
            });

            //给关联按钮绑定单击事件
            $("#saveBundActivityBtn").on("click",function () {
                //获取选中的按钮
                var checkedList = $("#tBody input:checked");

                //判断是否多余一个
                if (checkedList.size() === 0){
                    alert("至少关联一个");
                    return;
                }

                //获取关联的市场活动
                var activityIdsAndClueId = "";
                $.each(checkedList,function (index,data) {

                    activityIdsAndClueId += "activityId="+$.trim(data.value)+"&";
                })
                //获取线索id
                var clueId = "${clue.id}";
                activityIdsAndClueId += "clueId="+clueId;


                //发送ajax请求
                $.ajax({
                    url:"workbench/clueRemark/saveActivityClueRelation.do",
                    data:activityIdsAndClueId,
                    type:"post",
                    success:function (data) {
                        if (data.code === 1){
                            alert("关联成功!");
                            //刷新留言数据列表
                            getActivityClue();
                            //关闭模态窗口
                            $("#bundModal").modal("hide");
                        }else{
                            alert(data.message);
                        }
                    }
                })

            })

            //给转换按钮绑定单击事件
            $("#convertBtn").on("click",function () {
                var clueId = "${clue.id}";
                window.location.href = "workbench/clue/convertPage.do?clueId="+clueId;
            })
        });

        /**
         * 修改留言
         * @param id 留言id
         * @param noteContent 留言内容
         */
        function editClueRemark(id, noteContent) {
            //打开模态窗口
            $("#editRemarkModal").modal("show");

            //将数据放到对应的div框中
            $("#edit-noteContent").val(noteContent);
            $("#edit-id").val(id);

        }

        /**
         * 删除线索留言
         * @param id 线索留言id
         */
        function deleteClueRemark(id) {
            if (!confirm("您确定要删除这条数据吗?")) {
                alert("您已经取消删除了这条数据");
                return;
            }
            //发送ajax请求删除
            $.ajax({
                url: "workbench/clueRemark/deleteClueRemark.do",
                type: "post",
                data: {
                    id: id
                },
                success: function (data) {
                    if (data.code === 1) {
                        alert("删除成功!");
                        //刷新数据
                        getClueRemark();
                    } else {
                        alert(data.message);
                    }
                }
            })
            alert(id);
        }

        /**
         * 根据id删除线索和市场活动的关联
         * @param id
         */
        function deleteClueActivity(id) {
            //获取线索id
            var clueId = "${clue.id}";

            //发送ajax请求
            $.ajax({
                url:"workbench/clueRemark/deleteActivityClueRelation.do",
                type:"post",
                data:{
                    activityId:id,
                    clueId:clueId
                },
                success:function (data) {
                    if(data.code === 1){
                        alert("删除成功!");
                        //刷新界面
                        getActivityClue();
                    }else{
                        alert(data.message);
                    }
                }
            })
        }

        /**
         * 获取线索留言数据
         */
        function getClueRemark() {
            //获取线索的id
            var clueId = "${clue.id}";

            //发送ajax请求获取留言数据
            $.ajax({
                url: "workbench/clueRemark/getClueMark.do",
                type: "get",
                data: {
                    clueId: clueId
                },
                success: function (data) {
                    //拼接字符串
                    var htmlStr = "";
                    var fullName = "${clue.fullName}"
                    var appellation = "${clue.appellation}"
                    var company = "${clue.company}"
                    $.each(data, function (index, item) {
                        htmlStr += "<div class=\"remarkDiv\" style=\"height: 60px;\">" +
                            "<img title=\"zhangsan\" src=\"image/user-thumbnail.png\" style=\"width: 30px; height:30px;\">" +
                            "<div style=\"position: relative; top: -40px; left: 40px;\" >" +
                            "<h5>" + item.noteContent + "</h5>" +
                            "<font color=\"gray\">线索</font> <font color=\"gray\">-</font> <b>" + fullName + "-" + appellation + "" + company + "</b> <small style=\"color: gray;\"> " + item.createTime + "由" + item.createBy + "</small>" +
                            "<div style=\"position: relative; left: 500px; top: -30px; height: 30px; width: 100px; ;\">";
                        htmlStr += "<a class=\"myHref\" href=\"javascript:void(0);\"><span class=\"glyphicon glyphicon-edit\" onclick=\"editClueRemark('" + item.id + "','" + item.noteContent + "')\" style=\"font-size: 20px; color: #E6E6E6;\"></span></a>";
                        htmlStr += "&nbsp;&nbsp;&nbsp;&nbsp;";
                        htmlStr += "<a class=\"myHref\" href=\"javascript:void(0);\"><span class=\"glyphicon glyphicon-remove\"  onclick=\"deleteClueRemark('" + item.id + "')\"  style=\"font-size: 20px; color: #E6E6E6;\"></span></a>" +
                            "</div>" +
                            "</div>" +
                            "</div>";
                    });
                    //放到对应的div框中
                    $("#remarks").html(htmlStr);

                }
            })

            //拼接字符串
        }

        /**
         * 获取关联的市场活动
         */
        function getActivityClue() {
            //获取线索的id
            var clueId = "${clue.id}";
            //发送ajax请求获取
            $.ajax({
                url: "workbench/clueRemark/getActivityClue.do",
                type: "get",
                data: {
                    clueId: clueId
                },
                success: function (data) {
                    var htmlStr = "";
                    $.each(data, function (index, item) {
                        htmlStr += "<tr>";
                        htmlStr += "<td>" + item.name + "</td>";
                        htmlStr += "<td>" + item.startDate + "</td>";
                        htmlStr += "<td>" + item.endDate + "</td>";
                        htmlStr += "<td>" + item.owner + "</td>";
                        htmlStr += "<td><a href=\"javascript:void(0);\"  onclick=\"deleteClueActivity('"+item.id+"')\" style=\"text-decoration: none;\" ><span class=\"glyphicon glyphicon-remove\"></span>解除关联</a></td>";
                        htmlStr += "</tr>";
                    });
                    $("#relationTBody").html(htmlStr);
                }
            })
            //拼接字符串

        }
    </script>

</head>
<body>

<!-- 修改市场活动备注的模态窗口 -->
<div class="modal fade" id="editRemarkModal" role="dialog">
    <%-- 备注的id --%>
    <input type="hidden" id="remarkId">
    <div class="modal-dialog" role="document" style="width: 40%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">修改备注</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">
                    <input type="hidden" id="edit-id">
                    <div class="form-group">
                        <label for="edit-noteContent" class="col-sm-2 control-label">内容</label>
                        <div class="col-sm-10" style="width: 81%;">
                            <textarea class="form-control" rows="3" id="edit-noteContent"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="updateRemarkBtn">更新</button>
            </div>
        </div>
    </div>
</div>

<!-- 关联市场活动的模态窗口 -->
<div class="modal fade" id="bundModal" role="dialog">
    <div class="modal-dialog" role="document" style="width: 80%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title">关联市场活动</h4>
            </div>
            <div class="modal-body">
                <div class="btn-group" style="position: relative; top: 18%; left: 8px;">
                    <form class="form-inline" role="form">
                        <div class="form-group has-feedback">
                            <input id="searchActivityName" type="text" class="form-control" style="width: 300px;"
                                   placeholder="请输入市场活动名称，支持模糊查询">
                            <span class="glyphicon glyphicon-search form-control-feedback"></span>
                        </div>
                    </form>
                </div>
                <table id="activityTable" class="table table-hover" style="width: 900px; position: relative;top: 10px;">
                    <thead>
                    <tr style="color: #B3B3B3;">
                        <td><input type="checkbox" id="chkedAll"/></td>
                        <td>名称</td>
                        <td>开始日期</td>
                        <td>结束日期</td>
                        <td>所有者</td>
                        <td></td>
                    </tr>
                    </thead>
                    <tbody id="tBody">

                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button id="saveBundActivityBtn" type="button" class="btn btn-primary">关联</button>
            </div>
        </div>
    </div>
</div>


<!-- 返回按钮 -->
<div style="position: relative; top: 35px; left: 10px;">
    <a href="javascript:void(0);" onclick="window.history.back();"><span class="glyphicon glyphicon-arrow-left"
                                                                         style="font-size: 20px; color: #DDDDDD"></span></a>
</div>

<!-- 大标题 -->
<div style="position: relative; left: 40px; top: -30px;">
    <div class="page-header">
        <h3>${clue.fullName}${clue.appellation} <small>${clue.company}</small></h3>
    </div>
    <div style="position: relative; height: 50px; width: 500px;  top: -72px; left: 700px;">
        <button id="convertBtn" type="button" class="btn btn-default"><span class="glyphicon glyphicon-retweet"></span>
            转换
        </button>

    </div>
</div>

<br/>
<br/>
<br/>

<!-- 详细信息 -->
<div style="position: relative; top: -70px;">
    <div style="position: relative; left: 40px; height: 30px;">
        <div style="width: 300px; color: gray;">名称</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;">
            <b>${clue.fullName}${clue.appellation}</b></div>
        <div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">所有者</div>
        <div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${clue.owner}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 10px;">
        <div style="width: 300px; color: gray;">公司</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${clue.company}</b></div>
        <div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">职位</div>
        <div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${clue.job}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 20px;">
        <div style="width: 300px; color: gray;">邮箱</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${clue.email}</b></div>
        <div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">公司座机</div>
        <div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${clue.phone}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 30px;">
        <div style="width: 300px; color: gray;">公司网站</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${clue.website}</b></div>
        <div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">手机</div>
        <div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${clue.mphone}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 40px;">
        <div style="width: 300px; color: gray;">线索状态</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${clue.state}</b></div>
        <div style="width: 300px;position: relative; left: 450px; top: -40px; color: gray;">线索来源</div>
        <div style="width: 300px;position: relative; left: 650px; top: -60px;"><b>${clue.source}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px;"></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -60px; left: 450px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 50px;">
        <div style="width: 300px; color: gray;">创建者</div>
        <div style="width: 500px;position: relative; left: 200px; top: -20px;"><b>${clue.createBy}&nbsp;&nbsp;</b><small
                style="font-size: 10px; color: gray;">${clue.createTime}</small></div>
        <div style="height: 1px; width: 550px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 60px;">
        <div style="width: 300px; color: gray;">修改者</div>
        <div style="width: 500px;position: relative; left: 200px; top: -20px;"><b>${clue.editBy}&nbsp;&nbsp;</b><small
                style="font-size: 10px; color: gray;">${clue.editTime}</small></div>
        <div style="height: 1px; width: 550px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 70px;">
        <div style="width: 300px; color: gray;">描述</div>
        <div style="width: 630px;position: relative; left: 200px; top: -20px;">
            <b>
                ${clue.description}
            </b>
        </div>
        <div style="height: 1px; width: 850px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 80px;">
        <div style="width: 300px; color: gray;">联系纪要</div>
        <div style="width: 630px;position: relative; left: 200px; top: -20px;">
            <b>
                ${clue.contactSummary}
            </b>
        </div>
        <div style="height: 1px; width: 850px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 90px;">
        <div style="width: 300px; color: gray;">下次联系时间</div>
        <div style="width: 300px;position: relative; left: 200px; top: -20px;"><b>${clue.nextContactTime}</b></div>
        <div style="height: 1px; width: 400px; background: #D5D5D5; position: relative; top: -20px; "></div>
    </div>
    <div style="position: relative; left: 40px; height: 30px; top: 100px;">
        <div style="width: 300px; color: gray;">详细地址</div>
        <div style="width: 630px;position: relative; left: 200px; top: -20px;">
            <b>
                ${clue.address}
            </b>
        </div>
        <div style="height: 1px; width: 850px; background: #D5D5D5; position: relative; top: -20px;"></div>
    </div>
</div>

<!-- 备注 -->
<div id="remarkDivList" style="position: relative; top: 30px; left: 40px;">
    <div class="page-header">
        <h4>备注</h4>
    </div>

    <div id="remarks">


    </div>


    <div id="remarkDiv" style="background-color: #E6E6E6; width: 870px; height: 90px;">
        <form role="form" style="position: relative;top: 10px; left: 10px;">
            <textarea id="remark" class="form-control" style="width: 850px; resize : none;" rows="2"
                      placeholder="添加备注..."></textarea>
            <p id="cancelAndSaveBtn" style="position: relative;left: 737px; top: 10px; display: none;">
                <button id="cancelBtn" type="button" class="btn btn-default">取消</button>
                <button id="saveClueRemarkBtn" type="button" class="btn btn-primary">保存</button>
            </p>
        </form>
    </div>
</div>

<!-- 市场活动 -->
<div>
    <div style="position: relative; top: 60px; left: 40px;">
        <div class="page-header">
            <h4>市场活动</h4>
        </div>
        <div style="position: relative;top: 0px;">
            <table class="table table-hover" style="width: 900px;">
                <thead>
                <tr style="color: #B3B3B3;">
                    <td>名称</td>
                    <td>开始日期</td>
                    <td>结束日期</td>
                    <td>所有者</td>
                    <td></td>
                </tr>
                </thead>
                <tbody id="relationTBody">

                </tbody>
            </table>
        </div>

        <div>
            <a id="bundActivityBtn" href="javascript:void(0);" style="text-decoration: none;"><span
                    class="glyphicon glyphicon-plus"></span>关联市场活动</a>
        </div>
    </div>
</div>


<div style="height: 200px;"></div>
</body>
</html>